package com.it2297.renderer;

import java.awt.Color;
import java.awt.Component;
import java.sql.Timestamp;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

import com.it2297.model.UsedItem;

@SuppressWarnings("serial")
public class BiddersCellRenderer extends JLabel implements TableCellRenderer
{
	private Object[] data;
	private UsedItem bidItem;
	private String nric;
	private Timestamp latestDateBid;
	private int biddersNum;

	public BiddersCellRenderer()
	{
		this.setOpaque(true);
		this.setVerticalTextPosition(JLabel.TOP);
		this.setVerticalAlignment(JLabel.TOP);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object object, boolean isSelected, boolean hasFocus, int row, int column)
	{
		data = (Object[]) object;
		bidItem = (UsedItem) data[0];
		nric = (String) data[1];
		latestDateBid = (Timestamp) data[2];
		biddersNum = (Integer) data[3];

		switch (column)
		{
			case 0:
				this.setIcon(new ImageIcon(System.getProperty("user.dir") + "/img/used-item/" + bidItem.getUuid() + "-0-thumbnail.png"));

				if (bidItem.getDescription().length() > 128)
				{
					this.setText("<html><body><small><strong>" + bidItem.getTitle() + "</strong></small><br/><small>" + bidItem.getDescription().substring(0, 128) + "...</small></body></html>");
				}
				else
				{
					this.setText("<html><body><small><strong>" + bidItem.getTitle() + "</strong></small><br/><small>" + bidItem.getDescription() + "</small></body></html>");
				}
				break;
			case 1:
				this.setIcon(null);
				this.setText("<html><body><small><strong>" + biddersNum + "</strong></small></html>");
				;
				break;
			case 2:
				this.setIcon(null);
				this.setText("<html><body><small><strong>" + nric + "</strong></small><br/><small>" + latestDateBid.toString() + "</small></body></html>");
				break;
		}

		if (row % 2 == 0)
		{
			this.setBackground(Color.WHITE);
		}
		else
		{
			this.setBackground(new Color(240, 240, 240));
		}
		this.setForeground(Color.BLACK);

		if (isSelected)
		{
			this.setForeground(Color.WHITE);
			this.setBackground(new Color(80, 80, 80));
		}

		return this;
	}

}
